import ConfigParser
import data

class Medals:
    def __init__(self):
        self.parser = ConfigParser.ConfigParser()
        fp = open(data.filepath('medals.ini'))
        self.parser.readfp(fp)
        fp.close()
        self.jj = 0
        self.tmn = 0
        self.sd = 0
        self.ec = 0
        self.wave = 0
        self.npup = 0
        self.popup = []
    
    def update(self):
        p = self.parser
        if self.npup >= p.getint('PowerStar', 'param'):
            if not p.getboolean('PowerStar', 'done'):
                self.parser.set('PowerStar', 'done', 'true')
                self.popup.append('PowerStar')
        if self.wave >= p.getint('Tutorial', 'param'):
            if not p.getboolean('Tutorial', 'done'):
                self.parser.set('Tutorial', 'done', 'true')
                self.popup.append('Tutorial')
            if self.wave >= p.getint('Dedicated', 'param'):
                if not p.getboolean('Dedicated', 'done'):
                    self.parser.set('Dedicated', 'done', 'true')
                    self.popup.append('Dedicated')
        if self.sd >= p.getint('HighScore', 'param'):
            if not p.getboolean('HighScore', 'done'):
                self.parser.set('HighScore', 'done', 'true')
                self.parser.set('HighScore', 'param', "%d" % self.sd)
                print "Starting HS"
                self.popup.append('HighScore')

        if self.ec >= p.getint('EnemyCrusher', 'param'):
            if not p.getboolean('EnemyCrusher', 'done'):
                self.parser.set('EnemyCrusher', 'done', 'true')
                print "Starting EC"
                self.popup.append('EnemyCrusher')
        if self.jj >= p.getint('JumpingJack', 'param'):
            if not p.getboolean('JumpingJack', 'done'):
                self.parser.set('JumpingJack', 'done', 'true') 
                #start the display animation 
                print "Starting JJ"
                self.popup.append('JumpingJack')
            #additional medal with same parameters
            if self.jj >= p.getint('Jumper', 'param'):
                if not p.getboolean('Jumper', 'done'):
                    self.parser.set('Jumper', 'done', 'true') 
                    self.popup.append('Jumper')

        if self.tmn >= p.getint('TouchMeNot', 'param'):
            if not p.getboolean('TouchMeNot', 'done'):
                self.parser.set('TouchMeNot', 'done', 'true') 
                self.popup.append('TouchMeNot')
                print "Starting TMN"

        if self.sd >= p.getint('StarDust', 'param'):
            if not p.getboolean('StarDust', 'done'):
                self.parser.set('StarDust', 'done', 'true') 
                print "Starting SD"
                self.popup.append('StarDust')

    def save(self):
        self.parser.set('HighScore', 'done', 'false')
        if self.sd > self.parser.getint('HighScore', 'param'):
            self.parser.set('HighScore', 'param', "%d" % self.sd)
        #self.parser.set('StarDust', 'done', 'false')
        fp = open(data.filepath('medals.ini'), 'w')
        self.parser.write(fp)
        fp.close()

